/* eslint valid-jsdoc: "off" */
const path = require('path');
const { Database } = require('../project.config');
require('@babel/register');

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = (appInfo) => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {
    // cluster: {
    //   listen: {
    //     port: 7001,
    //   },
    // },
    //校验数据插件
    validate: {
      convert: true,
    },
  });

  // 腾讯云 serverless 配置
  config.env = 'prod';
  config.rundir = '/tmp';
  config.logger = {
    dir: '/tmp',
  };

  config.bodyParser = {
    formLimit: '30mb',
    jsonLimit: '30mb',
    textLimit: '30mb',
    // 值的大小可以根据自己的需求修改 这里只做演示
  };

  config.multipart = {
    mode: 'stream',
    whitelist: [
      // images
      '.jpg',
      '.jpeg', // image/jpeg
      '.png', // image/png, image/x-png
      '.gif', // image/gif
      '.bmp', // image/bmp
      '.wbmp', // image/vnd.wap.wbmp
      '.webp',
      '.tif',
      '.psd',
      // text
      '.txt',
      '.svg',
      '.js',
      '.jsx',
      '.json',
      '.css',
      '.less',
      '.html',
      '.htm',
      '.xml',
      // tar
      '.zip',
      '.gz',
      '.tgz',
      '.gzip',
      // video
      '.mp3',
      '.mp4',
      '.avi',
    ],
    fileSize: '20mb', // 最大5mb
  };

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1593691918681_6330';

  // add your middleware config here
  config.middleware = ['ipCheck', 'responseOption', 'statusRespose', 'isLogin'];

  config.isLogin = {
    ignore: [
      '/api/shop/user/wxlogin',
      '/api/registerAdmin',
      '/api/loginAdmin',
      '/api/register',
      '/api/login',
      '/api/shop/carousel/get',
      '/api/shop/createLogistics',
      '/api/shop/wechatpay/notify',
    ],
  };

  config.security = {
    csrf: {
      enable: false,
      ignoreJSON: true,
    },
  };

  // 禁止以下ip访问
  config.forbidIp = [
    // "192.168.1.1"
  ];

  config.cors = {
    origin: '*',
    allowMethods: 'GET,POST,PUT,DELETE,UPDATE',
    credentials: true,
  };

  // jwt token加密
  config.jwt = {
    secret: '12WmA)3aI_=%bc!@#',
  };
  // bcrypt 密码加密
  config.bcrypt = {
    saltRounds: 10,
  };

  // add your user config here
  const userConfig = {
    myAppName: 'egg',
  };

  config.sequelize = {
    dialect: 'mysql',
    host: Database.host,
    port: Database.port,
    database: Database.database,
    username: Database.username,
    password: Database.password,
    define: {
      underscored: true,
      underscoredAll: true,
      freezeTableName: true,
    },
    timezone: '+08:00',
    timestamps: true,
    paranoid: true,
  };

  config.static = {
    prefix: '/public/',
    dir: [path.join(appInfo.baseDir, 'app/public')],
  };

  return {
    ...config,
    ...userConfig,
  };
};
